package main

import (
	"fmt"
	"io"
	"strings"
)

func main() {
	{
		reader := strings.NewReader("hello world 我爱你中国")
		println(reader.Len()) //bytes number
		//fori不会越界
		for i := 0; i < reader.Len(); i++ {
			b, err := reader.ReadByte() //卧槽 这就有意思了，后面的汉字读不到
			if err != nil {
				fmt.Println(err.Error())
				break
			}
			print(b, "-", i, " ")
		}
		println()
		fmt.Println(strings.Repeat("*", 18))
	}

	{
		reader := strings.NewReader("hello world 我爱你中国")
		println(reader.Len()) //bytes number
		//fori不会越界
		i := 0
		for {
			b, err := reader.ReadByte()
			if err != nil {
				if err == io.EOF {
					fmt.Println("\nEOF at:", i)
				} else {
					fmt.Println(err.Error())
				}
				break
			}
			print(b, "-", i, " ")
			//print(string(b), "- ")
			i++
		}
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		reader := strings.NewReader("hello world 我爱你中国")
		println(reader.Len()) //bytes number
		//fori不会越界
		i := 0
		for {
			//size是这个rune占了几个byte
			r, size, err := reader.ReadRune()
			if err != nil {
				if err == io.EOF {
					fmt.Println("\nEOF at:", i)
				} else {
					fmt.Println(err.Error())
				}
				break
			}
			print(string(r), "-", size, " ")
			//fmt.Printf("%s-%d ", string(r), size)
			i++
		}
		fmt.Println(strings.Repeat("*", 18))
	}

	{
		count := strings.Count("abc", "ac")
		fmt.Println(count)
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		cmp := strings.Compare("abc", "ac")
		fmt.Println(cmp)
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		contains := strings.Contains("abc", "ac")
		fmt.Println(contains) //false
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		contains := strings.ContainsAny("abc", "ac")
		fmt.Println(contains) //true
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		contains := strings.ContainsRune("abc", '你')
		fmt.Println(contains) //false
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		show := func(s, sep string) {
			//Cut切一次。返回之前和之后的内容
			before, after, found := strings.Cut(s, sep)
			fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
		}
		show("Gopher", "Go")
		show("Gopher", "ph")
		show("Gopher", "er")
		show("Gopher", "Badger")
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		//么意思，就是Cut的特殊版本
		show := func(s, sep string) {
			after, found := strings.CutPrefix(s, sep)
			fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found)
		}
		show("Gopher", "Go")
		show("Gopher", "ph")
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		//么意思，就是Cut的特殊版本
		show := func(s, sep string) {
			after, found := strings.CutSuffix(s, sep)
			fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, after, found)
		}
		show("Gopher", "Go")
		show("Gopher", "er")
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		ss := strings.Split(strings.Repeat("abc", 5), "b")
		fmt.Println(ss)
		fmt.Printf("%q\n", strings.Split("a,b,c", ","))
		fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
		fmt.Printf("%q\n", strings.Split(" xyz ", ""))
		fmt.Printf("%q\n", strings.Split("xyz你好", "")) //有意思，完全拆解
		fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		fields := strings.Fields(" a man a plan a canal panama \t x")
		fmt.Println(fields)
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		//EqualFold: a more general form of case-insensitivity
		ef1 := strings.EqualFold("AB", "ab") //true
		ef2 := strings.EqualFold("α", "Α")   //true
		fmt.Println(ef1, ef2)

		fmt.Println(strings.Repeat("*", 18))
	}
	{
		up := strings.ToUpper("abABαΑ")  //
		low := strings.ToLower("abABαΑ") //
		fmt.Println(up, low)

		fmt.Println(strings.Repeat("*", 18))
	}
	{
		i := strings.Index("hello world, 你好中国", "l好") //-1
		//任意一个字符
		j := strings.IndexAny("hello world, 你好中国", "l好") //2
		fmt.Println(i, j)
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		a := strings.HasPrefix("hello world, 你好中国", "hello") //
		b := strings.HasSuffix("hello world, 你好中国", "中国")    //
		fmt.Println(a, b)
		fmt.Println(strings.Repeat("*", 18))
	}
	{
		//s := strings.Replace("hello world, 你好中国", "hello", "HELLO", 0) // 0不变
		s := strings.Replace("hello world, 你好中国", "你好", "很好", -1) // 0不变
		fmt.Println(s)
		fmt.Println(strings.Repeat("*", 18))
	}
}
